﻿$(function () {
    function Guid() {
        var g = "";
        var i = 32;
        while (i--) {
            g += Math.floor(Math.random() * 16.0).toString(16);
        }
        return g;
    };
    function WriteCookie(cookieName, cookieValue, expiry) {  //js写cookie
      var expDate = new Date();
      if (expiry) {
        expDate.setTime(expDate.getTime() + expiry);
        document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/;expires=" + expDate.toGMTString();
      }
      else {
          document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/;";
      }
    }
    function ReadCookie(name) {  //js读取cookie
        var cookieValue = "";
        var search = name + "=";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1)
                    end = document.cookie.length;
                cookieValue = unescape(document.cookie.substring(offset, end));
            }
        }
        return cookieValue;
    }           
 
    DateFormatString = function(datetime, format) {
        var year = datetime.getFullYear();
        var month = datetime.getMonth()+1;
        var date = datetime.getDate();
        var hour = datetime.getHours();
        var minutes = datetime.getMinutes();
        var second = datetime.getSeconds();
        format = format.replace(/YY/g, year).replace(/MM/g, month).replace(/DD/g, date).replace(/hh/g, hour).replace(/mm/g, minutes).replace(/ss/g, second);
        return format;
    };
 
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;  //判断浏览器是否支持cookie
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
        document.cookie = "testcookie";
        cookieEnabled = (document.cookie == "testcookie") ? true : false;
        document.cookie = "";
    }
    var firstshow = false;
    var visittotal;
    var isLogin;
    var loginName;
    if (cookieEnabled == true) {
        var HBCountShowCookie = ReadCookie("HBCountShowCookie"); //访客浏览器唯一ID
        var HBCountVisitTotalCookie = ReadCookie("HBCountVisitTotalCookie"); //访客浏览次数
        //var HBClientIDCookie = ReadCookie("HBClientIDCookie");    //访客上一个点击页面ID
        var HBLoginNameCookie = ReadCookie("HBLoginNameCookie");  //记录登录的用户名
        if (HBCountShowCookie == "" || HBCountShowCookie == null) {
            sparetime = 1000 * 60 * 60 * 24 * 3650; //有效期为一年
            WriteCookie('HBCountShowCookie', Guid(), sparetime);
            HBCountShowCookie = ReadCookie("HBCountShowCookie");
            firstshow = true;
        }
        else {
            firstshow = false;
        }
        if (HBCountVisitTotalCookie == "") { //游客统计
            sparetime = 1000 * 60 * 60 * 24 * 3650;
            WriteCookie('HBCountVisitTotalCookie', 1, sparetime);
            visittotal = 1;
        }
        else {
            if (firstshow == true) {
                visittotal = parseInt(HBCountVisitTotalCookie) + 1;
                sparetime = 1000 * 60 * 60 * 24 * 3650;
                WriteCookie('HBCountVisitTotalCookie', visittotal, sparetime);
            }
            else {
                visittotal = parseInt(HBCountVisitTotalCookie);
            }
        }
        try {
           if (HBStat != undefined || HBStat.isLogin != undefined || HBStat.loginName != undefined) { //此为登录用户统计
                if (HBStat.isLogin == 1 && HBStat.loginName != "") {
                    sparetime = 1000 * 60 * 60 * 24 * 120;
                    WriteCookie('HBLoginNameCookie', HBStat.loginName, sparetime);
                    HBLoginNameCookie = ReadCookie("HBLoginNameCookie");
                }
                isLogin=HBStat.isLogin;
            }
            else{isLogin=0;}
        }
        catch (e) { isLogin=0; }
        loginName=HBLoginNameCookie;
    } else {  //不支持cookie
        firstshow = false;
        visittotal = 1;
        var HBCountShowCookie = "";
        isLogin=0;
        loginName="";
    }
    var d = new Date();
    var currdate=DateFormatString(d,'YY-MM-DD hh:mm:ss');
    var screenwidth=screen.width;
    var screenheight=screen.height;
    var screencolordepth = screen.colorDepth;
    var menuId = $("#visitingstatisticsjs").attr("menuId");
    var nodeId = $("#visitingstatisticsjs").attr("nodeId");
    $.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js",
        function () {
            $.get("/portal/adapter/tracker.aspx",
            {
                "log.menuId": menuId,
                "log.nodeId": nodeId,
                "log.clientTime": currdate,
                //isLogin: isLogin,
                //loginName: loginName,
                "log.FirstView": firstshow,
                //VisitTotal: visittotal,
                "log.cookid": HBCountShowCookie,
                "log.resolution": screenwidth.toString() + "*" + screenheight.toString(),
                "log.screencolordepth": screencolordepth,
                "log.refUrl": document.referrer,
                "log.currentUrl":location.href,
                "log.isp": remote_ip_info["isp"],
                "log.country": remote_ip_info["country"],
                "log.province": remote_ip_info["province"],
                "log.city": remote_ip_info["city"],
                "log.district": remote_ip_info["district"]
            });
        });
})